package com.xuxueli.applyModules.utils.testcharts;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ExcelUtil
 * @Description: TODO
 **/
@Slf4j
public class ExcelUtil {

    public static List<Object[]> getData(InputStream in, String name, int sheetNum, int headRowNum) {
        List<Object[]> dataList;
        try {
            Workbook workbook;
            Sheet sheet = null;
            if (name.endsWith(".xls")) {
                workbook = new HSSFWorkbook(in);
                sheet = workbook.getSheetAt(sheetNum - 1);
            } else if (name.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(in);
                sheet = workbook.getSheetAt(sheetNum - 1);
            }
            dataList = getSheetData(sheet, headRowNum);
        } catch (Exception e) {
            return null;
        }
        return dataList;
    }

    public static List<Object[]> getSheetData(Sheet sheet, int headRowNum) {
        if (sheet == null) {
            return null;
        }
        int rowNum = sheet.getLastRowNum();
        if (rowNum <= headRowNum - 1) {
            log.warn("空文件");
            return null;
        }
        String[] headValues = getHeadCellValues(sheet.getRow(headRowNum - 1));
        if (headValues == null || headValues.length == 0) {
            return null;
        }
        int columnNum = headValues.length;
        Row row;
        List<Object[]> dataList = new ArrayList<>();
        Object[] rowData;
        for (int index = headRowNum; index <= sheet.getLastRowNum(); index++) {
            row = sheet.getRow(index);
            rowData = getRowCellValues(row, columnNum);
            if (rowData != null) {
                dataList.add(rowData);
            }
        }
        return dataList;
    }

    public static String[] getHeadCellValues(Row row) {
        if (row == null) {
            return null;
        }
        Object cellValue;
        List<String> headValues = new ArrayList<>();
        for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {
            cellValue = getCellValue(row.getCell(cellIndex));
            if (cellValue != null && StringUtils.isNotEmpty(cellValue.toString().trim())) {
                headValues.add(cellValue.toString().trim());
            }
        }
        String[] headers = new String[headValues.size()];
        return headValues.toArray(headers);
    }

    public static Object[] getRowCellValues(Row row, int columnNum) {
        if (row == null) {
            return null;
        }
        Object[] cellValues = new Object[columnNum];
        int emptyCount = 0;
        Object cellValue;
        for (int cellIndex = 0; cellIndex < columnNum; cellIndex++) {
            cellValue = getCellValue(row.getCell(cellIndex));
            cellValues[cellIndex] = cellValue;
            if (cellValue == null || StringUtils.isEmpty(cellValue.toString().trim())) {
                emptyCount++;
            }
        }
        if (emptyCount == columnNum) {
            return null;
        }
        return cellValues;
    }

    private static Object getCellValue(Cell cell) {
        if (cell == null) {
            return null;
        }
        Object cellValue;
        switch (cell.getCellType()) {
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case BOOLEAN:
                cellValue = cell.getBooleanCellValue();
                break;
            case NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    cellValue = cell.getDateCellValue();
                } else {
                    cell.setCellType(CellType.STRING);
                    cellValue = cell.getStringCellValue();
                }
                break;
            default:
                cellValue = null;
        }
        return cellValue;
    }

}